package com.brooks.demo;

import java.util.concurrent.CountDownLatch;

/**
 * @Author mihutu
 * @Date 2020/10/10 14:45
 * @Version 1.0
 */
public class DemoCacheLine2 {
    /**
     * 循环次数
     */
    public static long COUNT = 1_0000_0000L;

    /**
     * 这里要注意的是long类型占8字节
     */
    private static class T {
        public volatile long p1,p2,p3,p4,p5,p6,p7;
        public volatile long x = 0L;
        public volatile long q1,q2,q3,q4,q5,q6,q7;
    }

    public static T[] arr = new T[2];

    static {
        arr[0] = new T();
        arr[1] = new T();
    }

    public static void main(String[] args) throws Exception {
        CountDownLatch latch = new CountDownLatch(2);
        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < COUNT; i++) {
                arr[0].x = i;
            }
            latch.countDown();
        });
        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < COUNT; i++) {
                arr[1].x = i;
            }
            latch.countDown();
        });
        final long start = System.nanoTime();
        thread1.start();
        thread2.start();
        latch.await();
        System.out.println((System.nanoTime() - start) / 100_0000);
    }
}
